//
//  GradientLabel.m
//  QiBaoTong
//
//  Created by 韩艳锋 on 2019/6/18.
//  Copyright © 2019 策驭. All rights reserved.
//

#import "GradientLabel.h"
#import "Masonry.h"

@implementation GradientLabel {
    UILabel * _label;
    UIView * _shapView;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
-(instancetype)init{
    self = [super init];
    if (self) {
        _shapView = [[UIView alloc]init];
        _shapView.backgroundColor = [UIColor clearColor];
        [self addSubview:_shapView];
        
        _label = [[UILabel alloc]init];
        _label.textColor = [UIColor blackColor];
        [self addSubview:_label];
        [self.layer addSublayer:_label.layer];
        self.layer.mask = _label.layer;
    }
    return self;
}
-(void)setFont:(UIFont *)font {
    _label.font = font;
    [_label sizeToFit];
}

-(UIFont*)font{
    return _label.font;
}

-(void)setText:(NSString *)text{
    _label.text = text;
    [_label sizeToFit];
}

-(NSString*)text {
    return _label.text;
}

-(void)layoutSubviews {
    [super layoutSubviews];
    _shapView.frame = _label.layer.bounds;
    
    [_shapView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo([NSNumber numberWithDouble:self->_label.layer.bounds.size.width]);
        make.height.equalTo([NSNumber numberWithDouble:self->_label.layer.bounds.size.height]);
        make.width.height.top.left.equalTo(self);
    }];
}

+(instancetype)creatGradientLabel:(UIFont*)font{
    GradientLabel* obj = [[self alloc]init];
    obj.font = font;
    return obj;
}

@end
